<template>
    <div class="box-card box-minheight">
        <div class="card-header">
            <span>{{ curRouteName }}</span>
        </div>
        <div class="box-content">
            <el-form ref="form" :model="formData" :rules="formRules" label-width="120px" label-position="right">
                <div class="port-list">
                    <el-row :gutter="20">
                        <el-col :span="8">
                            <div class="port-list-item">
                                <div class="port-list-item-title">串口1</div>
                                <div class="port-list-item-content">
                                    <el-form-item label="波特率：">
                                        <el-select v-model="formData.baudrate" placeholder="请选择波特率">
                                            <el-option label="9600" :value="9600"></el-option>
                                            <el-option label="2400" :value="2400"></el-option>
                                            <el-option label="4800" :value="4800"></el-option>
                                            <!-- <el-option label="19200" :value="19200"></el-option> -->
                                            <el-option label="38400" :value="38400"></el-option>
                                            <!-- <el-option label="57600" :value="57600"></el-option> -->
                                            <el-option label="115200" :value="115200"></el-option>
                                            <!-- <el-option label="230400" :value="230400"></el-option>
                                            <el-option label="460800" :value="460800"></el-option>
                                            <el-option label="500000" :value="500000"></el-option>
                                            <el-option label="921600" :value="921600"></el-option>
                                            <el-option label="2000000" :value="2000000"></el-option>
                                            <el-option label="2500000" :value="2500000"></el-option>
                                            <el-option label="3000000" :value="3000000"></el-option> -->
                                        </el-select>
                                    </el-form-item>

                                    <el-form-item label="数据位：">
                                        <el-select v-model="formData.databit" placeholder="请选择数据位">
                                            <el-option label="6" :value="6"></el-option>
                                            <el-option label="7" :value="7"></el-option>
                                            <el-option label="8" :value="8"></el-option>
                                        </el-select>
                                    </el-form-item>

                                    <el-form-item label="校验位：">
                                        <el-select v-model="formData.parity" placeholder="请选择校验位">
                                            <el-option label="n" :value="0"></el-option>
                                            <el-option label="o" :value="1"></el-option>
                                            <el-option label="e" :value="2"></el-option>
                                        </el-select>
                                    </el-form-item>

                                    <el-form-item label="停止位：">
                                        <el-select v-model="formData.stopbit" placeholder="请选择停止位">
                                            <el-option label="1" :value="1"></el-option>
                                            <!-- <el-option label="1.5" :value="1.5"></el-option> -->
                                            <el-option label="2" :value="2"></el-option>
                                        </el-select>
                                    </el-form-item>

                                    <el-form-item label="协议类型" prop="protocolType">
                                        <el-select v-model="formData.protocolType">
                                            <el-option label="Modbus TCP" value="Modbus TCP"></el-option>
                                            <el-option label="Modbus RTU" value="Modbus RTU"></el-option>
                                            <!-- <el-option label="YBN" value="YBN"></el-option> -->
                                        </el-select>
                                    </el-form-item>

                                </div>
                            </div>
                        </el-col>
                        <el-col :span="8">
                            <div class="port-list-item">
                                <div class="port-list-item-title">串口2</div>
                                <div class="port-list-item-content">
                                    <el-form-item label="波特率：">
                                        <el-select v-model="formData.baudrate" placeholder="请选择波特率">
                                            <el-option label="9600" :value="9600"></el-option>
                                            <el-option label="2400" :value="2400"></el-option>
                                            <el-option label="4800" :value="4800"></el-option>
                                            <!-- <el-option label="19200" :value="19200"></el-option> -->
                                            <el-option label="38400" :value="38400"></el-option>
                                            <!-- <el-option label="57600" :value="57600"></el-option> -->
                                            <el-option label="115200" :value="115200"></el-option>
                                            <!-- <el-option label="230400" :value="230400"></el-option>
                                            <el-option label="460800" :value="460800"></el-option>
                                            <el-option label="500000" :value="500000"></el-option>
                                            <el-option label="921600" :value="921600"></el-option>
                                            <el-option label="2000000" :value="2000000"></el-option>
                                            <el-option label="2500000" :value="2500000"></el-option>
                                            <el-option label="3000000" :value="3000000"></el-option> -->
                                        </el-select>
                                    </el-form-item>

                                    <el-form-item label="数据位：">
                                        <el-select v-model="formData.databit" placeholder="请选择数据位">
                                            <el-option label="6" :value="6"></el-option>
                                            <el-option label="7" :value="7"></el-option>
                                            <el-option label="8" :value="8"></el-option>
                                        </el-select>
                                    </el-form-item>

                                    <el-form-item label="校验位：">
                                        <el-select v-model="formData.parity" placeholder="请选择校验位">
                                            <el-option label="n" :value="0"></el-option>
                                            <el-option label="o" :value="1"></el-option>
                                            <el-option label="e" :value="2"></el-option>
                                        </el-select>
                                    </el-form-item>

                                    <el-form-item label="停止位：">
                                        <el-select v-model="formData.stopbit" placeholder="请选择停止位">
                                            <el-option label="1" :value="1"></el-option>
                                            <!-- <el-option label="1.5" :value="1.5"></el-option> -->
                                            <el-option label="2" :value="2"></el-option>
                                        </el-select>
                                    </el-form-item>

                                    <el-form-item label="协议类型" prop="protocolType">
                                        <el-select v-model="formData.protocolType">
                                            <el-option label="Modbus TCP" value="Modbus TCP"></el-option>
                                            <el-option label="Modbus RTU" value="Modbus RTU"></el-option>
                                            <!-- <el-option label="YBN" value="YBN"></el-option> -->
                                        </el-select>
                                    </el-form-item>
                                </div>
                            </div>
                        </el-col>
                        <el-col :span="8">
                            <div class="port-list-item">
                                <div class="port-list-item-title">串口3</div>
                                <div class="port-list-item-content">
                                    <el-form-item label="波特率：">
                                        <el-select v-model="formData.baudrate" placeholder="请选择波特率">
                                            <el-option label="9600" :value="9600"></el-option>
                                            <el-option label="2400" :value="2400"></el-option>
                                            <el-option label="4800" :value="4800"></el-option>
                                            <!-- <el-option label="19200" :value="19200"></el-option> -->
                                            <el-option label="38400" :value="38400"></el-option>
                                            <!-- <el-option label="57600" :value="57600"></el-option> -->
                                            <el-option label="115200" :value="115200"></el-option>
                                            <!-- <el-option label="230400" :value="230400"></el-option>
                                            <el-option label="460800" :value="460800"></el-option>
                                            <el-option label="500000" :value="500000"></el-option>
                                            <el-option label="921600" :value="921600"></el-option>
                                            <el-option label="2000000" :value="2000000"></el-option>
                                            <el-option label="2500000" :value="2500000"></el-option>
                                            <el-option label="3000000" :value="3000000"></el-option> -->
                                        </el-select>
                                    </el-form-item>

                                    <el-form-item label="数据位：">
                                        <el-select v-model="formData.databit" placeholder="请选择数据位">
                                            <el-option label="6" :value="6"></el-option>
                                            <el-option label="7" :value="7"></el-option>
                                            <el-option label="8" :value="8"></el-option>
                                        </el-select>
                                    </el-form-item>

                                    <el-form-item label="校验位：">
                                        <el-select v-model="formData.parity" placeholder="请选择校验位">
                                            <el-option label="n" :value="0"></el-option>
                                            <el-option label="o" :value="1"></el-option>
                                            <el-option label="e" :value="2"></el-option>
                                        </el-select>
                                    </el-form-item>

                                    <el-form-item label="停止位：">
                                        <el-select v-model="formData.stopbit" placeholder="请选择停止位">
                                            <el-option label="1" :value="1"></el-option>
                                            <!-- <el-option label="1.5" :value="1.5"></el-option> -->
                                            <el-option label="2" :value="2"></el-option>
                                        </el-select>
                                    </el-form-item>

                                    <el-form-item label="协议类型" prop="protocolType">
                                        <el-select v-model="formData.protocolType">
                                            <el-option label="Modbus TCP" value="Modbus TCP"></el-option>
                                            <el-option label="Modbus RTU" value="Modbus RTU"></el-option>
                                            <!-- <el-option label="YBN" value="YBN"></el-option> -->
                                        </el-select>
                                    </el-form-item>
                                </div>
                            </div>
                        </el-col>
                        <el-col :span="8">
                            <div class="port-list-item">
                                <div class="port-list-item-title">串口4</div>
                                <div class="port-list-item-content">
                                    <el-form-item label="波特率：">
                                        <el-select v-model="formData.baudrate" placeholder="请选择波特率">
                                            <el-option label="9600" :value="9600"></el-option>
                                            <el-option label="2400" :value="2400"></el-option>
                                            <el-option label="4800" :value="4800"></el-option>
                                            <!-- <el-option label="19200" :value="19200"></el-option> -->
                                            <el-option label="38400" :value="38400"></el-option>
                                            <!-- <el-option label="57600" :value="57600"></el-option> -->
                                            <el-option label="115200" :value="115200"></el-option>
                                            <!-- <el-option label="230400" :value="230400"></el-option>
                                            <el-option label="460800" :value="460800"></el-option>
                                            <el-option label="500000" :value="500000"></el-option>
                                            <el-option label="921600" :value="921600"></el-option>
                                            <el-option label="2000000" :value="2000000"></el-option>
                                            <el-option label="2500000" :value="2500000"></el-option>
                                            <el-option label="3000000" :value="3000000"></el-option> -->
                                        </el-select>
                                    </el-form-item>

                                    <el-form-item label="数据位：">
                                        <el-select v-model="formData.databit" placeholder="请选择数据位">
                                            <el-option label="6" :value="6"></el-option>
                                            <el-option label="7" :value="7"></el-option>
                                            <el-option label="8" :value="8"></el-option>
                                        </el-select>
                                    </el-form-item>

                                    <el-form-item label="校验位：">
                                        <el-select v-model="formData.parity" placeholder="请选择校验位">
                                            <el-option label="n" :value="0"></el-option>
                                            <el-option label="o" :value="1"></el-option>
                                            <el-option label="e" :value="2"></el-option>
                                        </el-select>
                                    </el-form-item>

                                    <el-form-item label="停止位：">
                                        <el-select v-model="formData.stopbit" placeholder="请选择停止位">
                                            <el-option label="1" :value="1"></el-option>
                                            <!-- <el-option label="1.5" :value="1.5"></el-option> -->
                                            <el-option label="2" :value="2"></el-option>
                                        </el-select>
                                    </el-form-item>

                                    <el-form-item label="协议类型" prop="protocolType">
                                        <el-select v-model="formData.protocolType">
                                            <el-option label="Modbus TCP" value="Modbus TCP"></el-option>
                                            <el-option label="Modbus RTU" value="Modbus RTU"></el-option>
                                            <!-- <el-option label="YBN" value="YBN"></el-option> -->
                                        </el-select>
                                    </el-form-item>
                                </div>
                            </div>
                        </el-col>
                        <el-col :span="8">
                            <div class="port-list-item">
                                <div class="port-list-item-title">串口5</div>
                                <div class="port-list-item-content">
                                    <el-form-item label="波特率：">
                                        <el-select v-model="formData.baudrate" placeholder="请选择波特率">
                                            <el-option label="9600" :value="9600"></el-option>
                                            <el-option label="2400" :value="2400"></el-option>
                                            <el-option label="4800" :value="4800"></el-option>
                                            <!-- <el-option label="19200" :value="19200"></el-option> -->
                                            <el-option label="38400" :value="38400"></el-option>
                                            <!-- <el-option label="57600" :value="57600"></el-option> -->
                                            <el-option label="115200" :value="115200"></el-option>
                                            <!-- <el-option label="230400" :value="230400"></el-option>
                                            <el-option label="460800" :value="460800"></el-option>
                                            <el-option label="500000" :value="500000"></el-option>
                                            <el-option label="921600" :value="921600"></el-option>
                                            <el-option label="2000000" :value="2000000"></el-option>
                                            <el-option label="2500000" :value="2500000"></el-option>
                                            <el-option label="3000000" :value="3000000"></el-option> -->
                                        </el-select>
                                    </el-form-item>

                                    <el-form-item label="数据位：">
                                        <el-select v-model="formData.databit" placeholder="请选择数据位">
                                            <el-option label="6" :value="6"></el-option>
                                            <el-option label="7" :value="7"></el-option>
                                            <el-option label="8" :value="8"></el-option>
                                        </el-select>
                                    </el-form-item>

                                    <el-form-item label="校验位：">
                                        <el-select v-model="formData.parity" placeholder="请选择校验位">
                                            <el-option label="n" :value="0"></el-option>
                                            <el-option label="o" :value="1"></el-option>
                                            <el-option label="e" :value="2"></el-option>
                                        </el-select>
                                    </el-form-item>

                                    <el-form-item label="停止位：">
                                        <el-select v-model="formData.stopbit" placeholder="请选择停止位">
                                            <el-option label="1" :value="1"></el-option>
                                            <!-- <el-option label="1.5" :value="1.5"></el-option> -->
                                            <el-option label="2" :value="2"></el-option>
                                        </el-select>
                                    </el-form-item>
                                    <el-form-item label="协议类型" prop="protocolType">
                                        <el-select v-model="formData.protocolType">
                                            <el-option label="Modbus TCP" value="Modbus TCP"></el-option>
                                            <el-option label="Modbus RTU" value="Modbus RTU"></el-option>
                                            <!-- <el-option label="YBN" value="YBN"></el-option> -->
                                        </el-select>
                                    </el-form-item>
                                </div>
                            </div>
                        </el-col>
                        <el-col :span="8">
                            <div class="port-list-item">
                                <div class="port-list-item-title">串口6</div>
                                <div class="port-list-item-content">
                                    <el-form-item label="波特率：">
                                        <el-select v-model="formData.baudrate" placeholder="请选择波特率">
                                            <el-option label="9600" :value="9600"></el-option>
                                            <el-option label="2400" :value="2400"></el-option>
                                            <el-option label="4800" :value="4800"></el-option>
                                            <!-- <el-option label="19200" :value="19200"></el-option> -->
                                            <el-option label="38400" :value="38400"></el-option>
                                            <!-- <el-option label="57600" :value="57600"></el-option> -->
                                            <el-option label="115200" :value="115200"></el-option>
                                            <!-- <el-option label="230400" :value="230400"></el-option>
                                            <el-option label="460800" :value="460800"></el-option>
                                            <el-option label="500000" :value="500000"></el-option>
                                            <el-option label="921600" :value="921600"></el-option>
                                            <el-option label="2000000" :value="2000000"></el-option>
                                            <el-option label="2500000" :value="2500000"></el-option>
                                            <el-option label="3000000" :value="3000000"></el-option> -->
                                        </el-select>
                                    </el-form-item>

                                    <el-form-item label="数据位：">
                                        <el-select v-model="formData.databit" placeholder="请选择数据位">
                                            <el-option label="6" :value="6"></el-option>
                                            <el-option label="7" :value="7"></el-option>
                                            <el-option label="8" :value="8"></el-option>
                                        </el-select>
                                    </el-form-item>

                                    <el-form-item label="校验位：">
                                        <el-select v-model="formData.parity" placeholder="请选择校验位">
                                            <el-option label="n" :value="0"></el-option>
                                            <el-option label="o" :value="1"></el-option>
                                            <el-option label="e" :value="2"></el-option>
                                        </el-select>
                                    </el-form-item>

                                    <el-form-item label="停止位：">
                                        <el-select v-model="formData.stopbit" placeholder="请选择停止位">
                                            <el-option label="1" :value="1"></el-option>
                                            <!-- <el-option label="1.5" :value="1.5"></el-option> -->
                                            <el-option label="2" :value="2"></el-option>
                                        </el-select>
                                    </el-form-item>
                                    <el-form-item label="协议类型" prop="protocolType">
                                        <el-select v-model="formData.protocolType">
                                            <el-option label="Modbus TCP" value="Modbus TCP"></el-option>
                                            <el-option label="Modbus RTU" value="Modbus RTU"></el-option>
                                            <!-- <el-option label="YBN" value="YBN"></el-option> -->
                                        </el-select>
                                    </el-form-item>
                                </div>
                            </div>
                        </el-col>

                    </el-row>
                </div>
            </el-form>
            <div class="btn-box">
                <el-button type="primary" @click="submitForm">确定</el-button>
            </div>
        </div>
    </div>
</template>

<script setup lang="ts">
import { useRoute } from 'vue-router'
import { ref, computed } from 'vue'
//获取路由对象
let $route = useRoute()
// 计算出当前路由名称
let curRouteName = computed(() => {
    return $route.matched[$route.matched.length - 1].meta.title
})

const form = ref(null)
const formData = ref({
    baudrate: 2400,
    databit: 6,
    parity: 1,
    stopbit: 1,
    protocolType: 'Modbus TCP'
});

const validatePassword = (rule: any, value: string, callback: any) => {
    const pattern = /^[a-zA-Z0-9]{4,12}$/;
    if (!pattern.test(value)) {
        callback(new Error('新密码必须由4-12位数字或字母组成'));
    } else {
        callback();
    }
};

const validateConfirmPassword = (rule: any, value: string, callback: any) => {
    if (value !== formData.value.password) {
        callback(new Error('两次输入的密码不一致'));
    } else {
        callback();
    }
};
const formRules = {
    username: [
        { required: true, message: '请输入用户名', trigger: 'change' },
    ],
    password: [
        { required: true, message: '请输入新密码', trigger: 'change' },
        { validator: validatePassword, trigger: 'change' },
    ],
    confirmPassword: [
        { required: true, message: '请再次输入新密码', trigger: 'change' },
        { validator: validateConfirmPassword, trigger: 'change' },
    ],
};



const submitForm = () => {
    form.value.validate((valid: boolean) => {
        if (valid) {
            // 表单验证通过，可以进行提交操作
            // ...
        } else {
            // 表单验证不通过，进行错误处理
            return false;
        }
    });
};


</script>

<style lang="scss" scoped>
.btn-box {
    padding: 20px 0;
    display: flex;
    justify-content: center;
}

.port-list {
    .port-list-item-title {
        margin-bottom: 15px;
        font-size: 16px;
        font-weight: bold;
    }

    .port-list-item-content {
        border: 1px solid #dedede;
        padding: 10px;
        border-radius: 10px;
        margin-bottom: 25px;
    }
}
</style>
